package com.health.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JsonConfig;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.health.constant.Constants;
import com.health.model.LoginInfo;
import com.health.util.DateJsonValueProcessor;

/**
 * @author DirkMurphy
 * 
 * @blog http://www.cnblogs.com/dirkmurphyjava/
 * 
 * @date
 * 
 * @introduction
 * 	All actions Unify Entrance
 */
public abstract class ActionInterface extends DispatchAction
{
	public static JsonConfig	jsonConfig	= new JsonConfig();
	static
	{
		initConverter(jsonConfig);
	}

	public static void initConverter(JsonConfig jsonConfig)
	{
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,
				new DateJsonValueProcessor());
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception
	{
		ActionForward actionForward = null;
		boolean b = validateSession(request, response);
		if (b == false)
		{
			response.sendRedirect("user-reg-login.jsp");
			return null;
		}

		String parameter = mapping.getParameter();
		String name = getMethodName(mapping, form, request, response, parameter);
		actionForward = dispatchMethod(mapping, form, request, response, name);
		return actionForward;
	}

	
	public boolean validateSession(HttpServletRequest request,
			HttpServletResponse response) throws IOException
	{
		HttpSession session = request.getSession();
		LoginInfo loginInfo = (LoginInfo) session.getAttribute(Constants.LOGININFO);
		if (null == loginInfo)
		{
			return false;
		}
		return true;
	}
}
